var App = getApp()
var get = require("../../model/getopenid.js");
Page({

  /**
   * 页面的初始数据
   */

  data: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

  },
  formSubmit: function(event) {
    var payment = event.detail.value.pay;
    // this.setData({cost: payment});
      console.log( '11111:',App.globalData.openid, App.globalData.storied, App.globalData.nickname)
   wx.request({
      url: 'https://www.greatforworld.com/pay',
      data: {
        openid: App.globalData.openid,
        storied: App.globalData.storied,
        nickname: App.globalData.nickname,
        cost: payment
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: function(res) {
        var orderId = res.data.data.orderId;
        wx.request({
          url: 'https://www.greatforworld.com/wx/', //仅为示例，并非真实的接口地址
          data: {
            num: payment
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          success: function(res) {
            if (res.data.code === 0) {
              wx.requestPayment({
                'timeStamp': res.data.data.timeStamp + '',
                'nonceStr': res.data.data.nonceStr,
                'package': `prepay_id=${res.data.data.prepay_id}`,
                'signType': 'MD5',
                'paySign': res.data.data.paySignStr,
                'success': function(res1) {
                  console.log('支付成功:',res1)
                  wx.request({
                    url: 'https://www.greatforworld.com/paySuccess', //仅为示例，并非真实的接口地址
                      data: {
                          orderId: orderId
                      },
                      method: "POST",
                      header: {
                          'content-type': 'application/x-www-form-urlencoded',
                          'Accept': 'application/json'
                      },
                      success: function(res) {
                      if (res.data.code === 0) {
                        console.log('hahhaahahh')
                      }
                      // 发送 res.code 到后台换取 openId, sessionKey, unionId

                    }
                  })
                },
                'fail': function(res2) {
                  console.log('fail:', res2)
                }
              })
            }

          }
        })
      }
    })
  },
  get_pay_info: function(event) {
    var payment = event.detail.value;
    this.setData({cost: payment});
    wx.request({
      url: 'http://127.0.0.1/pay',
      data: {
        openid: this.data.openid,
        user: this.data.user,
        store: "锦城大酒店",
        cost: this.data.cost
      },
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success: function(res) {
        console.log("success");
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
